package com.example.vaultmysql.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;

import java.time.LocalDateTime;

/**
 * Vault配置实体类
 * 
 * @author System
 * @version 1.0.0
 */
@Entity
@Table(name = "vault_config")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class VaultConfig {

    @Id
    @Column(length = 50)
    private String id;

    @Column(name = "vault_url", length = 255, nullable = false)
    private String vaultUrl;

    @Column(name = "auth_method", length = 50, nullable = false)
    private String authMethod = "approle";

    @Column(name = "namespace", length = 100)
    private String namespace;

    @Column(name = "role_id", length = 255)
    private String roleId;

    @Column(name = "enabled", nullable = false)
    private Boolean enabled = true;

    @CreationTimestamp
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    /**
     * 构造函数
     */
    public VaultConfig(String id, String vaultUrl, String authMethod, 
                      String namespace, String roleId) {
        this.id = id;
        this.vaultUrl = vaultUrl;
        this.authMethod = authMethod;
        this.namespace = namespace;
        this.roleId = roleId;
        this.enabled = true;
    }

    /**
     * 验证配置
     */
    public boolean isValid() {
        return vaultUrl != null && !vaultUrl.trim().isEmpty() &&
               authMethod != null && !authMethod.trim().isEmpty();
    }

    /**
     * 获取显示名称
     */
    public String getDisplayName() {
        return String.format("%s (%s)", vaultUrl, authMethod);
    }

    /**
     * 是否使用AppRole认证
     */
    public boolean isAppRoleAuth() {
        return "approle".equalsIgnoreCase(authMethod);
    }

    /**
     * 是否使用Token认证
     */
    public boolean isTokenAuth() {
        return "token".equalsIgnoreCase(authMethod);
    }
}